package com.example.workbench.service.impl;

import com.example.settings.dao.UserDao;
import com.example.settings.domain.User;
import com.example.utils.SqlSessionUtil;
import com.example.vo.PaginationVO;
import com.example.workbench.dao.ActivityDao;
import com.example.workbench.dao.ActivityRemarkDao;
import com.example.workbench.domain.Activity;
import com.example.workbench.service.ActivityService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ActivityServiceImpl implements ActivityService {

    private ActivityDao activityDao = SqlSessionUtil.getSqlSession().getMapper(ActivityDao.class);
    private ActivityRemarkDao activityRemarkDao = SqlSessionUtil.getSqlSession().getMapper(ActivityRemarkDao.class);
    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public boolean save(Activity a) {

        boolean flag = true;

        int count = activityDao.save(a);
        if(count!=1){

            flag = false;

        }

        return flag;
    }

    public PaginationVO<Activity> pageList(Map<String, Object> map) {
        //取得总记录条数
        int total = activityDao.getTotalByCondition(map);

        //取得dataList
        List<Activity> alist = activityDao.getActivityListByCondition(map);

        PaginationVO<Activity> vo = new PaginationVO();
        vo.setTotal(total);
        vo.setDataList(alist);

        return vo;
    }

    public Boolean delete(String[] ids) {
        Boolean flag = true;

        //查询市场活动备注数量
        int count1 = activityRemarkDao.getCountByActivityId(ids);
        //删除市场活动备注
        int count2 = activityRemarkDao.deleteByActivityId(ids);
        if (count1 != count2){
            flag = false;
        }
        //删除市场活动
        int count3 = activityDao.deleteActivityByid(ids);
        if (count3 != ids.length){
            flag = false;
        }
        return flag;
    }

    public Map<String, Object> getUserAndActivity(String id) {

        //取得用户
        List<User> userList = userDao.getUserList();
        //取得市场活动
        Activity a = activityDao.getActivity(id);
        //返回map
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("user",userList);
        map.put("a",a);
        return map;
    }

    public boolean update(Activity a) {

        Boolean flag = true;
        int count = activityDao.update(a);
        if (count != 1){
            flag=false;
        }
        return flag;
    }

    public Activity detail(String id) {
        Activity a = activityDao.detail(id);
        return a;
    }

    public List<Activity> getActivityListById(String clueId) {
        List<Activity> activityList = activityDao.getActivityListById(clueId);
        return activityList;
    }

    public List<Activity> getActivityListByNameAndNotByClueId(Map<String, String> map) {
        List<Activity> activityList = activityDao.getActivityListByNameAndNotByClueId(map);
        return activityList;
    }

    public List<Activity> getActivityListByName(String aname) {
        List<Activity> activityList = activityDao.getActivityListByName(aname);
        return activityList;
    }
}






















